package com.zeek.car90s;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.NativeExpressAdView;
import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.game.UMGameAgent;
import com.zeek.car90s.util.SoundPoolUtils;
import com.zeek.car90s.util.Tools;

public class FeelingLuckyActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnTotalCoin, btnBack, btnGold, btnSilver, btnCopper, btnIron, btnCoin;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feeling_lucky);

        //展示插屏
        if (Constants.mInterstitialAd.isLoaded()) {
            Constants.mInterstitialAd.show();
        }

        //展示原生广告
        NativeExpressAdView adView = (NativeExpressAdView) findViewById(R.id.adView);
        adView.loadAd(new AdRequest.Builder().build());

        //展示banner
        AdView mAdView = (AdView) findViewById(R.id.adView_bottom);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        sp = getSharedPreferences(Constants.SHARED_PREFERENCE_PREFIX, Context.MODE_PRIVATE);

        btnTotalCoin = (Button) findViewById(R.id.btn_game_coin);
        btnTotalCoin.setText(Constants.TOTAL_COIN + "");

        btnBack = (Button) findViewById(R.id.button_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnGold = (Button) findViewById(R.id.btn_gold);
        btnGold.setOnClickListener(this);

        btnSilver = (Button) findViewById(R.id.btn_silver);
        btnSilver.setOnClickListener(this);

        btnCopper = (Button) findViewById(R.id.btn_copper);
        btnCopper.setOnClickListener(this);

        btnIron = (Button) findViewById(R.id.btn_iron);
        btnIron.setOnClickListener(this);

        btnCoin = (Button) findViewById(R.id.btn_coin);

    }

    @Override
    public void onClick(View v) {
        int selectedId = v.getId();
        switch (selectedId) {
            case R.id.btn_gold:
            case R.id.btn_silver:
            case R.id.btn_copper:
            case R.id.btn_iron:
                getRandomCoins();
                break;
        }
    }

    //随机得到金币
    private void getRandomCoins() {
        int coin;
        double value = Math.random();
        if (value < 0.3) {
            coin = 1;
        } else if (value < 0.55) {
            coin = 2;
        } else if (value < 0.7) {
            coin = 3;
        } else if (value < 0.85) {
            coin = 4;
        } else if (value < 0.9) {
            coin = 4;
        } else if (value < 0.95) {
            coin = 4;
        } else if (value < 0.98) {
            coin = 5;
        } else {
            coin = 5;
        }

        Constants.TOTAL_COIN += coin;
        refreshCoin();
        SoundPoolUtils.play(1);

        btnGold.setVisibility(View.INVISIBLE);
        btnSilver.setVisibility(View.INVISIBLE);
        btnCopper.setVisibility(View.INVISIBLE);
        btnIron.setVisibility(View.GONE);

        btnCoin.setVisibility(View.VISIBLE);
        btnCoin.setText("+"+coin);

        //让金币慢慢消失
        AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);//创建一个AlphaAnimation 对象，渐变从1->0
        aa.setDuration(2500);//设置持续时间
        aa.setFillAfter(true);//设置这个View最后的状态，由于是从1->0,所以最后的是消失状态（最后是看不到见这个View的）
        btnCoin.startAnimation(aa);//启动动画

        UMGameAgent.bonus("Feeling Lucky", 1, coin, 4);
        MobclickAgent.onEvent(FeelingLuckyActivity.this, "feeling_lucky_get_coins");
    }

    private void refreshCoin() {
        btnTotalCoin.setText(Constants.TOTAL_COIN + "");

        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(Constants.COIN_PREFIX, Constants.TOTAL_COIN);
        editor.commit();

        if (Constants.TOTAL_COIN > 9999) {
            btnTotalCoin.getLayoutParams().width = (int) Tools.convertDpToPx(66, this);
        } else if (Constants.TOTAL_COIN > 999) {
            btnTotalCoin.getLayoutParams().width = (int) Tools.convertDpToPx(58, this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        //页面统计
        MobclickAgent.onPageStart("Feeling lucky");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd("Feeling lucky"); // 保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }

}
